﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_Flow_DeleteDocument_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_Flow_DeleteDocument_V2];
GO
CREATE PROCEDURE [dbo].[sproc_Flow_DeleteDocument_V2]
    @DocID int
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;

IF EXISTS(SELECT 1 FROM dbo.uds_flow_document WHERE doc_id = @DocID)
BEGIN
    -- Starts a new transaction.
    BEGIN TRANSACTION;

    --删除真正的消息
    DELETE FROM UDS_SMS_MobileMsgSendBuffer
        WHERE msgid in (
            SELECT msgid FROM uds_flow_message WHERE doc_id = @docid
        );

    --删除当前步骤的提醒
    DELETE FROM uds_flow_message WHERE doc_id = @docid;

    --删除批注
    DELETE FROM uds_flow_postil WHERE doc_id = @DocID;

    --删除路径备份
    DELETE FROM uds_flow_path WHERE doc_id = @DocID;

    --删除附件信息
    DELETE FROM  uds_flow_files WHERE docid = @DocID;

    --删除签收
    DELETE FROM uds_flow_status WHERE doc_id = @DocID;

    --删除批注的附件
    DELETE FROM  uds_flow_files
        WHERE fileid in (
            SELECT [file_id] FROM uds_flow_postil WHERE doc_id = @DocID
        );

    --删除文档信息
    DELETE FROM  uds_flow_document WHERE doc_id = @DocID;

    --删除表单
    DELETE FROM uds_flow_Style_Data WHERE doc_id = @DocID;

    -- Succeed!
    COMMIT TRANSACTION;
    RETURN 0;
END 
ELSE
BEGIN
    -- The document does not exist.
    RETURN -1;
END

END
GO